package com.lee.base;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2022/3/9
 */
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class IndexOperation {

    private static RestHighLevelClient client;
    @BeforeAll
    public static void init() {
        client = new RestHighLevelClient(RestClient.builder(HttpHost.create("https://elasticsearch-dev.souban.io")));
    }
    @Test
    @Order(1)
    public void create() throws IOException {
        CreateIndexResponse response = client.indices().create(new CreateIndexRequest("user"), RequestOptions.DEFAULT);
        System.out.println(response.toString());
        System.out.println(response.isAcknowledged());
    }

    @Test
    @Order(3)
    public void delete() throws IOException {
        AcknowledgedResponse response = client.indices().delete(new DeleteIndexRequest("my_java_index2"), RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }

    @Test
    @Order(2)
    public void query() throws IOException {
        GetIndexResponse getR = client.indices().get(new GetIndexRequest("my_java_index2"), RequestOptions.DEFAULT);
        System.out.println(getR.getMappings());
    }

    @AfterAll
    private static void destory() throws IOException {
        client.close();
    }
}
